package co.yixiang.huiMin.controller;


import co.yixiang.annotation.AnonymousAccess;
import co.yixiang.huiMin.dao.CarManageQueryCriteria;
import co.yixiang.huiMin.dao.MachinistQueryCriteria;
import co.yixiang.huiMin.entity.CarManage;
import co.yixiang.huiMin.entity.CarTypes;
import co.yixiang.huiMin.entity.Machinist;
import co.yixiang.huiMin.mapper.CarTypesMapper;
import co.yixiang.huiMin.repository.CarManageRepository;
import co.yixiang.huiMin.service.CarManageService;
import co.yixiang.modules.shop.service.dto.YxStoreCategoryDTO;
import co.yixiang.modules.shop.service.dto.YxUserDTO;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import com.alipay.api.domain.Car;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 车辆入驻 前端控制器
 * </p>
 *
 * @author Author
 * @since 2020-08-04
 */
@RestController
@Api(tags = "车辆入驻")
@RequestMapping("/carManage")
public class CarManageController {

   @Resource
   private CarManageService carManageService;

    @Resource
    private CarManageRepository carManageRepository;

    @Resource
    private CarTypesMapper carTypesMapper;
    /**
     * 查询车辆入驻
     * @return
     */
    @AnonymousAccess
    @ApiOperation("查询车辆入驻")
    @GetMapping("/selectCarManages")
    public ResponseEntity<Object> selectCarManages(Pageable pageable){


        CarManageQueryCriteria carManageQueryCriteria = new CarManageQueryCriteria();
        carManageQueryCriteria.setDelFlag(0);
        Page<CarManage> carManages = carManageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, carManageQueryCriteria, criteriaBuilder), pageable);
        List<CarManage> list = carManages.getContent();
        for (CarManage carManage : list) {
            QueryWrapper<CarTypes> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",carManage.getCarId());
            CarTypes carTypes=carTypesMapper.selectOne(queryWrapper);
            carManage.setCarTypeName(carTypes.getCarType());
        }
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("code",200);
        resultMap.put("message", PageUtil.toPage(carManages));
        return new ResponseEntity<>(resultMap, HttpStatus.OK);

    }




    /**
     *审核车辆入驻
     * @return
     */
    @AnonymousAccess
    @ApiOperation("审核车辆入驻")
    @PostMapping("/updateCarManages")
    public ResponseEntity<Object> updateCarTypes(@RequestBody CarManage carManage){

        HashMap<String, Object> resultMap = new HashMap<>();
        if(carManageService.updateById(carManage)){
            resultMap.put("code",200);
            return new ResponseEntity<>(resultMap, HttpStatus.OK);
        }
        resultMap.put("code",300);
        return new ResponseEntity<>(resultMap, HttpStatus.OK);
    }
}

